package com.xiaoshuidi.cloud.module.iot.dal.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.details.WaterMeterReportListModel;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDosageDo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Arvin
 * description:
 * date: 2018/11/19 10:33 AM
 **/

@Mapper
public interface DeviceDosageDoMapper extends BaseMapper<TbDeviceDosageDo> {
    /**
     * 根据设备编号和时间查询tb_device_dosage列表
     *
     * @return
     */

    @Select("SELECT  " +
            "            t.dosage_date AS dosageDate,  " +
            "            MIN(t.start_amount) AS startAmount,  " +
            "            MAX(t.end_amount) AS endAmount,  " +
            "            SUM(t.apportion_amount) AS apportionAmount,  " +
            "            SUM(t.total_amount) AS totalAmount  " +
            "        FROM  " +
            "          `tb_device_dosage` t" +
            " where ${ew.sqlSegment}")
    List<WaterMeterReportListModel> getDeviceDosageByDeviceNoAndTime(@Param("ew") QueryWrapper<?> wrapper);

    /**
     * 根据设备编号和时间查询tb_device_dosage列表(无分页)
     *
     * @return
     */
    @Select(" SELECT  *  FROM  tb_device_dosage t where ${ew.sqlSegment}")
    List<TbDeviceDosageDo> getDeviceDosageByDeviceNoAndTimeNoPage(@Param("ew") QueryWrapper<?> wrapper);


    /**
     * 根据deviceno查询日用量
     *
     * @return
     */
    @Select(" SELECT sum(total_amount) FROM tb_device_dosage where device_no=#{deviceNo} and dosage_date=str_to_date(now() - INTERVAL 1 DAY,'%Y-%m-%d') and status=1 ")
    Double getOneDayRead(String deviceNo);

    //查询用电明细
    @Select("SELECT" +
            " sum( amount ) amount," +
            " device_no deviceNo," +
            " dosage_date dosageDate," +
            " min( start_amount ) startAmount," +
            " max( end_amount ) endAmount," +
            " max(start_dosage_date) startDosageDate," +
            " max(total_amount) totalAmount" +
            " from tb_device_dosage t where ${ew.sqlSegment}")
    List<TbDeviceDosageDo> getDeviceDosage(@Param("ew") QueryWrapper<?> wrapper);

}
